﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SuperSocket.SocketBase;
using SuperSocket.SocketBase.Protocol;
using DeviceCommon;

namespace DeviceControlServer.ServerBase
{  
    public class ClientRequestPackSession 
        : AppSession<ClientRequestPackSession, BinaryRequestInfo>
    {
        /// <summary>
        /// 向客户端回复信息，按照回复协议定义的格式发送
        /// </summary>
        /// <param name="pack"></param>
        public void Send(ResponseMsgPackge pack)
        {
            byte[] dataBody = MsgPackHandler.ConvertToBytes<ResponseMsgPackge>(pack);          
        
            // 协议命令名称占2位
            var commandData = BitConverter.GetBytes((ushort)pack.ActionState);

            // 长度int类型占4位
            var dataLen = BitConverter.GetBytes(dataBody.Length);

            // 命令加内容长度为6
            var sendData = new byte[6 + dataBody.Length];

            // +-------+---+-------------------------------+
            // |response | l  |                               |
            // | name    | e |    response body      |
            // |  (2)        | n |                               |
            // |              |(4)|                               |
            // +-------+---+-------------------------------+
            Array.ConstrainedCopy(commandData, 0, sendData, 0, commandData.Length);
            Array.ConstrainedCopy(dataLen, 0, sendData, 2, 4);
            Array.ConstrainedCopy(dataBody, 0, sendData, 6, dataBody.Length);
            Send(sendData, 0, sendData.Length);
        }
    }
}
